# 正常异常混合模型
model2 = \
"""
model{
    for (n in 1:N){
        for (i in 1:I){
            for (k in 1:K){w[n,i,k]<-(att[n,k]>=Q[i,k])}
            eta[n,i] <- prod(w[n,i,1:K])
            logit(p[n,i]) <- (eta[n,i]*delta_i[i]+beta_i[i])
            Y[n,i] ~ dbern(p[n,i]*(1-flag[n,i])+d_c[i]*flag[n,i])
            rt_mu_mat[n,i] <- (xi[i]-ability[n,2])*(1-flag[n,i])+mu_c*flag[n,i]
            rt_var_mat[n,i] <- normal_rt_sig_den[i] *(1-flag[n,i])+var_c_den*flag[n,i]
            logT[n,i] ~ dnorm(rt_mu_mat[n,i],rt_var_mat[n,i])

            }
        }

    for (i in 1:I){
        item_parameter[i,1:3] ~ dmnorm(item_mean[1:3],item_den[,])
        delta_i[i] <- item_parameter[i,2]
        beta_i[i] <- item_parameter[i,1]
        xi[i] <- item_parameter[i,3]
        normal_rt_sig_den[i] ~ dgamma(1,1)
        omega[i] <- 1/(1/(normal_rt_sig_den[i]))^(0.5)
        }
    beta_i_mu ~ dnorm(-2.197,1/2)
    delta_i_mu ~ dnorm(4.397,1/2) T(0,)
    xi_mu ~ dnorm(3,1/2)
    item_mean[1] <- beta_i_mu
    item_mean[2] <- delta_i_mu
    item_mean[3] <- xi_mu
    item_den[1:3,1:3] ~ dwish(R1[,],3)
    item_cov <- inverse(item_den[,])


    for (n in 1:N){
        for (k in 1:K){
            logit(att_pi[n,k]) <- theta[n]*lambda_k[k]+lambda_0[k]
            att[n,k] ~ dbern(att_pi[n,k])
            }
        theta[n] <- ability[n,1]
        tau[n] <- ability[n,2]
        ability[n,1:2] ~ dmnorm(mu_ability[1:2],person_den[,])
        }

    for (k in 1:K){
        lambda_k[k] ~ dnorm(0,0.25) T(0,)
        lambda_0[k] ~ dnorm(0,0.25)
        }

    mu_ability[1] <- 0
    mu_ability[2] <- 0
    person_den[1:2,1:2] ~ dwish(R2[,],2)
    student_cov <- inverse(person_den[,])

    mu_c ~ dnorm(2,1/10)
    var_c_den ~ dgamma(1,1)
    var_c <- inverse(var_c_den)
    for (i in 1:I){
        d_c[i] ~ dbeta(1,1)
        }

    for (i in 1:I){
        for (n in 1:N){
            flag[n,i] ~ dbern(pi_[i])
        }
        pi_[i] ~ dbeta(1,5)
    }   
}"""


# 正常、快速猜测、题目预知混合模型
model1 = \
    """
model{
    for (n in 1:N){
        for (i in 1:I){
            for (k in 1:K){w[n,i,k]<-(att[n,k]>=Q[i,k])}
            eta[n,i] <- prod(w[n,i,1:K])
            logit(p[n,i]) <- (eta[n,i]*delta_i[i]+beta_i[i])
            Y[n,i] ~ dbern(p[n,i]*(flag[n,i]==1)+d_c[i]*(flag[n,i]==3)+d_g[i]*(flag[n,i]==2))
            rt_mu_mat[n,i] <- (xi[i]-ability[n,2])*(flag[n,i]==1)+mu_c*(flag[n,i]>1)
            rt_var_mat[n,i] <- normal_rt_sig_den[i]*(flag[n,i]==1)+var_c_den*(flag[n,i]>1)
            logT[n,i] ~ dnorm(rt_mu_mat[n,i],rt_var_mat[n,i])

        }
    }

    for (i in 1:I){
        item_parameter[i,1:3] ~ dmnorm(item_mean[1:3],item_den[,])
        delta_i[i] <- item_parameter[i,2]
        beta_i[i] <- item_parameter[i,1]
        xi[i] <- item_parameter[i,3]
        normal_rt_sig_den[i] ~ dgamma(1,1)
        omega[i] <- 1/(1/(normal_rt_sig_den[i]))^(0.5)
    }
    beta_i_mu ~ dnorm(-2.197,1/2)
    delta_i_mu ~ dnorm(4.397,1/2) T(0,)
    xi_mu ~ dnorm(3,1/2)
    item_mean[1] <- beta_i_mu
    item_mean[2] <- delta_i_mu
    item_mean[3] <- xi_mu
    item_den[1:3,1:3] ~ dwish(R1[,],3)
    item_cov <- inverse(item_den[,])


    for (n in 1:N){
        for (k in 1:K){
            logit(att_pi[n,k]) <- theta[n]*lambda_k[k]+lambda_0[k]
            att[n,k] ~ dbern(att_pi[n,k])
        }
        theta[n] <- ability[n,1]
        tau[n] <- ability[n,2]
        ability[n,1:2] ~ dmnorm(mu_ability[1:2],person_den[,])
    }

    for (k in 1:K){
        lambda_k[k] ~ dnorm(0,0.25) T(0,)
        lambda_0[k] ~ dnorm(0,0.25)
    }

    mu_ability[1] <- 0
    mu_ability[2] <- 0
    person_den[1:2,1:2] ~ dwish(R2[,],2)
    student_cov <- inverse(person_den[,])

    mu_c ~ dnorm(2,1/10)
    var_c_den ~ dgamma(1,1)
    var_c <- inverse(var_c_den)
    for (i in 1:I){
        d_c[i] ~ dunif(0.67,1)
        logit(d_g[i]) <- beta_i[i]
    }

    for (i in 1:I){
        for (n in 1:N){
            flag_aberrant[n,i] ~ dbern(pi_[i])
            flag[n,i] <- (flag_aberrant[n,i]==0)+(flag_aberrant[n,i]==1)*(flag_g_c[n]==1)*2+(flag_aberrant[n,i]==1)*(flag_g_c[n]==0)*3
        }
        pi_[i] ~ dbeta(1,5)
    }
    for (n in 1:N){
        flag_g_c[n] ~ dbern(pi_g_c[n])
        pi_g_c[n] ~ dbeta(1,1)
    }   
}"""

# 高阶正常异常混合模型
model4 = \
"""
model{
    for (n in 1:N){
        for (i in 1:I){
            for (k in 1:K){w[n,i,k]<-(att[n,k]>=Q[i,k])}
            eta[n,i] <- prod(w[n,i,1:K])
            logit(p[n,i]) <- (eta[n,i]*delta_i[i]+beta_i[i])
            Y[n,i] ~ dbern(p[n,i]*(1-flag[n,i])+d_c[i]*flag[n,i])
            rt_mu_mat[n,i] <- (xi[i]-ability[n,2])*(1-flag[n,i])+mu_c*flag[n,i]
            rt_var_mat[n,i] <- normal_rt_sig_den[i] *(1-flag[n,i])+var_c_den*flag[n,i]
            logT[n,i] ~ dnorm(rt_mu_mat[n,i],rt_var_mat[n,i])

            }
        }

    for (i in 1:I){
        item_parameter[i,1:3] ~ dmnorm(item_mean[1:3],item_den[,])
        delta_i[i] <- item_parameter[i,2]
        beta_i[i] <- item_parameter[i,1]
        xi[i] <- item_parameter[i,3]
        normal_rt_sig_den[i] ~ dgamma(1,1)
        omega[i] <- 1/(1/(normal_rt_sig_den[i]))^(0.5)
        }
    beta_i_mu ~ dnorm(-2.197,1/2)
    delta_i_mu ~ dnorm(4.397,1/2) T(0,)
    xi_mu ~ dnorm(3,1/2)
    item_mean[1] <- beta_i_mu
    item_mean[2] <- delta_i_mu
    item_mean[3] <- xi_mu
    item_den[1:3,1:3] ~ dwish(R1[,],3)
    item_cov <- inverse(item_den[,])


    for (n in 1:N){
        for (k in 1:K){
            logit(att_pi[n,k]) <- theta[n]*lambda_k[k]+lambda_0[k]
            att[n,k] ~ dbern(att_pi[n,k])
            }
        theta[n] <- ability[n,1]
        tau[n] <- ability[n,2]
        ability[n,1:2] ~ dmnorm(mu_ability[1:2],person_den[,])
        }

    for (k in 1:K){
        lambda_k[k] ~ dnorm(0,0.25) T(0,)
        lambda_0[k] ~ dnorm(0,0.25)
        }

    mu_ability[1] <- 0
    mu_ability[2] <- 0
    person_den[1:2,1:2] ~ dwish(R2[,],2)
    student_cov <- inverse(person_den[,])

    mu_c ~ dnorm(2,1/10)
    var_c_den ~ dgamma(1,1)
    var_c <- inverse(var_c_den)
    for (i in 1:I){
        d_c[i] ~ dbeta(1,1)
        }

    for (i in 1:I){
        for (n in 1:N){
            flag[n,i] ~ dbern(pi_[n,i])
            logit(pi_[n,i]) <- yita[n]+h[i]
        }
        
    }
    for (n in 1:N){
        
        yita[n] ~ dnorm(0,1)
        
    }

    for (i in 1:I){
        
        h[i] ~ dnorm(0,1)
        
    }   
}"""


# 高阶三行为混合模型
model3 = \
    """
model{
    for (n in 1:N){
        for (i in 1:I){
            for (k in 1:K){w[n,i,k]<-(att[n,k]>=Q[i,k])}
            eta[n,i] <- prod(w[n,i,1:K])
            logit(p[n,i]) <- (eta[n,i]*delta_i[i]+beta_i[i])
            Y[n,i] ~ dbern(p[n,i]*flag[n,i,1]+d_g[i]*flag[n,i,2]+d_c[i]*flag[n,i,3])
            rt_mu_mat[n,i] <- (xi[i]-ability[n,2])*(flag[n,i,1])+mu_c*sum(flag[n,i,2:3])
            rt_var_mat[n,i] <- normal_rt_sig_den[i]*flag[n,i,1]+var_c_den*sum(flag[n,i,2:3])
            logT[n,i] ~ dnorm(rt_mu_mat[n,i],rt_var_mat[n,i])

        }
    }

    for (i in 1:I){
        item_parameter[i,1:3] ~ dmnorm(item_mean[1:3],item_den[,])
        delta_i[i] <- item_parameter[i,2]
        beta_i[i] <- item_parameter[i,1]
        xi[i] <- item_parameter[i,3]
        normal_rt_sig_den[i] ~ dgamma(1,1)
        omega[i] <- 1/(1/(normal_rt_sig_den[i]))^(0.5)
    }
    beta_i_mu ~ dnorm(-2.197,1/2)
    delta_i_mu ~ dnorm(4.397,1/2) T(0,)
    xi_mu ~ dnorm(3,1/2)
    item_mean[1] <- beta_i_mu
    item_mean[2] <- delta_i_mu
    item_mean[3] <- xi_mu
    item_den[1:3,1:3] ~ dwish(R1[,],3)
    item_cov <- inverse(item_den[,])


    for (n in 1:N){
        for (k in 1:K){
            logit(att_pi[n,k]) <- theta[n]*lambda_k[k]+lambda_0[k]
            att[n,k] ~ dbern(att_pi[n,k])
        }
        theta[n] <- ability[n,1]
        tau[n] <- ability[n,2]
        ability[n,1:2] ~ dmnorm(mu_ability[1:2],person_den[,])
    }

    for (k in 1:K){
        lambda_k[k] ~ dnorm(0,0.25) T(0,)
        lambda_0[k] ~ dnorm(0,0.25)
    }

    mu_ability[1] <- 0
    mu_ability[2] <- 0
    person_den[1:2,1:2] ~ dwish(R2[,],2)
    student_cov <- inverse(person_den[,])

    mu_c ~ dnorm(2,1/10)
    var_c_den ~ dgamma(1,1)
    var_c <- inverse(var_c_den)
    for (i in 1:I){
        d_c[i] ~ dunif(0.67,1)
        logit(d_g[i]) <- beta_i[i]
    } 

    for(i in 1:I){
        for (n in 1:N){
            
            logit(flag[n,i,1]) <- yita[n,1]+h[i]
            flag[n,i,2] <- ilogit(yita[n,2])*flag[n,i,1]
            flag[n,i,3] <- 1-sum(flag[n,i,1:2])
        }
    }

    for (n in 1:N){
        for (c in 1:2){
            yita[n,c] ~ dnorm(0,1)
        }
    }

    for (i in 1:I){
            h[i] ~ dnorm(0,1)
    }
}"""

# 带有门的行为混合模型
model5 = \
    """
model{
    for (n in 1:N){
        for (i in 1:I){
            for (k in 1:K){w[n,i,k]<-(att[n,k]>=Q[i,k])}
            eta[n,i] <- prod(w[n,i,1:K])
            logit(p[n,i]) <- (eta[n,i]*delta_i[i]+beta_i[i])
            Y[n,i] ~ dbern(p[n,i]*(flag[n,i]==1)+d_c[i]*(flag[n,i]==3)+d_g[i]*(flag[n,i]==2))
            rt_mu_mat[n,i] <- (xi[i]-ability[n,2])*(flag[n,i]==1)+mu_c*(flag[n,i]>1)
            rt_var_mat[n,i] <- normal_rt_sig_den[i]*(flag[n,i]==1)+var_c_den*(flag[n,i]>1)
            logT[n,i] ~ dnorm(rt_mu_mat[n,i],rt_var_mat[n,i])

        }
    }

    for (i in 1:I){
        item_parameter[i,1:3] ~ dmnorm(item_mean[1:3],item_den[,])
        delta_i[i] <- item_parameter[i,2]
        beta_i[i] <- item_parameter[i,1]
        xi[i] <- item_parameter[i,3]
        normal_rt_sig_den[i] ~ dgamma(1,1)
        omega[i] <- 1/(1/(normal_rt_sig_den[i]))^(0.5)
    }
    beta_i_mu ~ dnorm(-2.197,1/2)
    delta_i_mu ~ dnorm(4.397,1/2) T(0,)
    xi_mu ~ dnorm(3,1/2)
    item_mean[1] <- beta_i_mu
    item_mean[2] <- delta_i_mu
    item_mean[3] <- xi_mu
    item_den[1:3,1:3] ~ dwish(R1[,],3)
    item_cov <- inverse(item_den[,])


    for (n in 1:N){
        for (k in 1:K){
            logit(att_pi[n,k]) <- theta[n]*lambda_k[k]+lambda_0[k]
            att[n,k] ~ dbern(att_pi[n,k])
        }
        theta[n] <- ability[n,1]
        tau[n] <- ability[n,2]
        ability[n,1:2] ~ dmnorm(mu_ability[1:2],person_den[,])
    }

    for (k in 1:K){
        lambda_k[k] ~ dnorm(0,0.25) T(0,)
        lambda_0[k] ~ dnorm(0,0.25)
    }

    mu_ability[1] <- 0
    mu_ability[2] <- 0
    person_den[1:2,1:2] ~ dwish(R2[,],2)
    student_cov <- inverse(person_den[,])

    mu_c ~ dnorm(2,1/10)
    var_c_den ~ dgamma(1,1)
    var_c <- inverse(var_c_den)
    for (i in 1:I){
        d_c[i] ~ dunif(0.67,1)
        # d_c[i] <- 0.9
        logit(d_g[i]) <- beta_i[i]
    }

    for (i in 1:I){
        for (n in 1:N){
            flag_aberrant[n,i] ~ dbern(pi_[i])
            flag[n,i] <- (flag_aberrant[n,i]==0)+(flag_aberrant[n,i]==1)*(flag_g_c[n,i]==1)*2+(flag_aberrant[n,i]==1)*(flag_g_c[n,i]==0)*3
        }
        pi_[i] ~ dbeta(1,5)
        item_gate[i] ~ dbeta(1,1)
    }
    for (n in 1:N){
        for (i in 1:I){
            flag_g_c[n,i] ~ dbern(1-item_gate[i]*(1-pi_g_c[n]))
        }
        pi_g_c[n] ~ dbeta(1,1)
    }   
}"""